package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.model.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/users")
    @ResponseBody
    public String getUsers(){
        List<User> userList =userService.findAll();
        String res="";
        for(User user:userList){
            System.out.println(user);
        res+=user.toString();
        }
        return  res;
    }
    @RequestMapping("/users2")
    public ModelAndView getUsersModelAndView(){
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("/WEB-INF/jsp/users.jsp");
        modelAndView.addObject("msg","Holle");
        return modelAndView;

    }
    @RequestMapping("/users3")
    public String getUsers3ModelAndView(){
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.addObject("msg","Holle");
        return "users";

    }
    @RequestMapping("/users4")
    public String getUsers4ModelAndView(HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("msg","hello request");
        return "users";

    }
    @RequestMapping("/users5")
    public void getUsers5ModelAndView( HttpServletResponse response) throws IOException {
        response.getWriter().print("hello httpservletresponse");


    }
    @RequestMapping("/users6")
    @ResponseBody
    public  String getUsers6ModelAndView(){
        return "{\"name\":\"zhangsan\",\"age\":18}";
    }
    @RequestMapping("/users7")
    @ResponseBody
    public  String getUsers7ModelAndView() throws JsonProcessingException {
        User user = new User();
        user.setName("wss");
        user.setSex("g");
        user.setAge(3);
        ObjectMapper objectMapper =new ObjectMapper();

        return  objectMapper.writeValueAsString(user);
    }
    @RequestMapping("/users8")
    @ResponseBody
    public  User getUsers8() {
        User user = new User();
        user.setName("wsss");
        user.setSex("g");
        user.setAge(3);


        return  user;
    }
    @RequestMapping("/users9")
    @ResponseBody
    public  List<User> getUsers9() {



        return userService.findAll();
    }
    @RequestMapping("/users10")
    @ResponseBody
    public  void getUsers10(String name,int age) {
        System.out.println(name);
        System.out.println(age);



    }
    @RequestMapping("/users11")
    @ResponseBody
    public  void getUsers10(User user) {
        System.out.println(user.getSex());
        System.out.println(user.getAge());
        System.out.println(user.getName());


    }
}
